package com.jxb.seuicscanlib;

import android.content.Context;
import android.os.RemoteException;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import com.seuic.scankey.IKeyEventCallback;
import com.seuic.scankey.ScanKeyService;
import com.seuic.scanner.DecodeInfo;
import com.seuic.scanner.DecodeInfoCallBack;
import com.seuic.scanner.Scanner;
import com.seuic.scanner.ScannerFactory;
import com.seuic.scanner.ScannerKey;

/**
 * @author jinxb
 * date     : 2021/7/23
 * desc     :
 */
public class ScannerManager {
    private Scanner scanner;
    private ScanKeyService mScanKeyService;
    private IKeyEventCallback mCallback;

    public static final  ScannerManager instance = new ScannerManager();


//    private static class Holder {
//        private final static ScannerManager sInstance = new ScannerManager();
//    }

    private ScannerManager() {
        mScanKeyService = ScanKeyService.getInstance();
        mCallback = new IKeyEventCallback.Stub() {
            @Override
            public void onKeyDown(int keyCode) {
                scanner.startScan();
            }

            @Override
            public void onKeyUp(int keyCode) {
                scanner.stopScan();
            }
        };
    }
    public static ScannerManager getInstance() {
        return  instance;
//        return Holder.sInstance;
    }

    /**
     * 打开摄像头
     */
    public void openScanner(Context context) {
        if (scanner == null) {
            mScanKeyService.registerCallback(mCallback, "148,149,248,249,250");
            scanner = ScannerFactory.getScanner(context);
            scanner.open();
        }
    }

    public void closeScanner() {
        if (scanner == null) {
            return;
        }
        scanner.setDecodeInfoCallBack(null);
        scanner.close();
        scanner = null;
    }

    public void setDecodeInfoCallBack(DecodeInfoCallBack callBack) {
        if (scanner != null) {
            scanner.setDecodeInfoCallBack(callBack);
        }
    }
}
